package com.lhd.qd.module.system.role.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.lhd.qd.common.base.BasePageQuery;
import com.lhd.qd.common.base.BaseServiceImpl;
import com.lhd.qd.common.consts.dict.DictTypeCodeEnum;
import com.lhd.qd.common.translator.DictTransRule;
import com.lhd.qd.common.translator.TransUtils;
import com.lhd.qd.module.system.role.dao.RoleMapper;
import com.lhd.qd.module.system.role.model.converter.AbstractRoleConverter;
import com.lhd.qd.module.system.role.model.dataobj.RoleDO;
import com.lhd.qd.module.system.role.model.dto.RoleSaveDTO;
import com.lhd.qd.module.system.role.model.vo.RoleDetailVO;
import com.lhd.qd.module.system.role.model.vo.RoleListVO;
import com.lhd.qd.module.system.role.service.RoleService;
import com.lhd.qd.module.system.user.model.vo.UserListVO;
import org.springframework.stereotype.Service;

@Service
public class RoleServiceImpl extends BaseServiceImpl<RoleMapper, RoleDO> implements RoleService {

    @Override
    public IPage<RoleListVO> pageRole(BasePageQuery query) {

        LambdaQueryWrapper<RoleDO> queryWrapper = Wrappers.<RoleDO>lambdaQuery();

        IPage<RoleDO> doPage = page(new Page<>(query.getCurrent(), query.getSize()), queryWrapper);

        IPage<RoleListVO> voPage = AbstractRoleConverter.INSTANCE.doPage2ListVOPage(doPage);

        TransUtils.trans(voPage.getRecords(), RoleListVO.class, new DictTransRule[]{
                new DictTransRule(DictTypeCodeEnum.ROLE_LEVEL, "levelCode")
        });

        return voPage;
    }

    @Override
    public RoleDetailVO getRoleById(Long id) {

        RoleDetailVO vo = AbstractRoleConverter.INSTANCE.do2DetailVO(getById(id));

        return vo;
    }

    @Override
    public boolean saveRole(RoleSaveDTO saveDTO) {

        RoleDO dataObj = AbstractRoleConverter.INSTANCE.saveDTO2DO(saveDTO);

        return save(dataObj);
    }

    @Override
    public boolean updateRoleById(Long id, RoleSaveDTO saveDTO) {

        RoleDO dataObj = AbstractRoleConverter.INSTANCE.saveDTO2DO(saveDTO);

        dataObj.setId(id);

        return updateById(dataObj);
    }

    @Override
    public boolean removeRoleById(Long id) {

        return false;
    }
}
